<?php
namespace App\Domain;

use App\Model\Agent as Model_Agent;

class Agent {
	
	
	/* 设置 */
	public function getMyCode($uid) {
		$rs=[];
        $model = new Model_Agent();
        $rs= $model->getMyCode($uid);
		return $rs;
	}
    

    /* 检测必填 */
	public function check($uid) {

        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $configpri=\App\getConfigPri();
        
        $isfill='0';
        $ismust='0';
        
        if($configpri['agent_switch']){
            
            $model = new Model_Agent();
            $info= $model->getAgentInfo($uid);
            if(!$info){
                $info2= $model->getAgentLower($uid);
                if($info2){
                    // 已有下级
                    $isfill='1';
                }
            }else{
                // 已填写邀请码
                $isfill='1';
            }
            
            if($configpri['agent_must']){
                $ismust='1';
            }
            
        }else{
           $isfill='1'; 
        }

        $userinfo=\App\getUserInfo($uid);

        $rs['info'][0]['ismust']=$ismust;
        $rs['info'][0]['isfill']=$isfill;
        $rs['info'][0]['openinstall_switch']=$configpri['openinstall_switch'];
        $rs['info'][0]['is_firstlogin']=$userinfo['is_firstlogin']; //iOS专用

		return $rs;
	}

    /* 设置 */
	public function setAgent($uid,$code) {

        $rs = array('code' => 0, 'msg' => '设置成功', 'info' => array());

        $model = new Model_Agent();
        $codeinfo= $model->getCode($code);
        
        if(!$codeinfo){
            $rs['code'] = 1003;
            $rs['msg'] = '邀请码错误';
            return $rs;	
        }
        
        $one_uid=$codeinfo['uid'];
        
        if($one_uid==$uid){
            $rs['code'] = 1004;
            $rs['msg'] = '不能填写自己的邀请码';
            return $rs;	
        }
        
        $agent_uid=$model->getAgentInfo($uid);
        if($agent_uid){
            $rs['code'] = 1005;
            $rs['msg'] = '已填写过邀请码';
            return $rs;	
        }
        
        $agent_uid=$model->getAgentLower($uid);
        if($agent_uid){
            $rs['code'] = 1005;
            $rs['msg'] = '已经有下级了，不能再填写';
            return $rs;	
        }
        
        $nowtime=time();
        $data=[
            'uid'=>$uid,
            'one'=>$one_uid,
            'addtime'=>$nowtime,
        ];
        
        $oneuid_agent=$model->getAgentInfo($one_uid);
        if($oneuid_agent){
            if($oneuid_agent['one']==$uid ){
                $rs['code'] = 1006;
                $rs['msg'] = '不能互相填写';
                return $rs;	
            }
            
        }

        $result=$model->setAgent($data);
        
        $this->setAgentReward($one_uid,$uid);

		return $rs;
	}
    
    /**
     * 邀请奖励
     * @param int $one_uid 上级用户ID
     * @param int $uid 下级用户ID
     */
    public function setAgentReward($one_uid,$uid){

        $rs = array('code' => 0, 'msg' => '设置成功', 'info' => array());

        $date = date("Ymd");
        
        $configpri=\App\getConfigPri();
        
        $agent_reward=$configpri['agent_reward'];
        $agent_daytimes=$configpri['agent_daytimes'];
        $agent_times=$configpri['agent_times'];
        
        $model = new Model_Agent();
        
        $info= $model->getLimit($one_uid);


        if($info){
            if($info['times']>=$agent_times){
                
                return $rs;
            }
            
            if( $info['addtime']==$date ){
                // 当天
                if( $agent_daytimes >0 && $info['daytimes']>=$agent_daytimes ){
                    
                    return $rs;
                }
                
                $model->upLimit($one_uid);
                
            }else{
                // 新一天
                $model->upLimitDay($one_uid,$date);
            }
            
        }else{
            $data=[
                'uid'=>$one_uid,
                'times'=>1,
                'daytimes'=>1,
                'addtime'=>$date,
            ];
            $model->setLimit($data);
        }

        if($agent_reward){
            \App\addVotes($one_uid,$agent_reward,$agent_reward);

            //添加映票收入记录
            $data=array(
                'type'=>1,
                'action'=>1, //注册邀请奖励
                'uid'=>$one_uid, //收入用户id
                'fromid'=>$uid, //贡献用户id
                'total'=>$agent_reward,
                'votes'=>$agent_reward,
                'addtime'=>time()
            );

            \App\addVoteRecord($data);

            //添加邀请关系收益
            $oneuid_profit=$model->getProfit($one_uid);

            if($oneuid_profit){
                $model->upProfit($one_uid,"one_p",$agent_reward);
            }else{
                $model->setProfit(['uid'=>$one_uid,'one_p'=>$agent_reward]);
            }

            $uid_profit=$model->getProfit($uid);
            if($uid_profit){
                $model->upContri($uid,$agent_reward);
            }else{
                $model->setProfit(['uid'=>$uid,'one'=>$agent_reward]);
            }

        }
        
        return 1;
        
    }
	
}
